#!/bin/bash
#basefun=`which basefunction.sh`
basefun="/usr/local/bin/basefunction.sh"
parseIni=`which parseini.sh`
upCfgFile=`which fs.qcom-update.ini`
if [ "$basefun" != "" ] && [ "$parseIni" != "" ] ;then
	. $basefun
else
	echo -e "cant find basefunction.sh or parseini.sh\n exit ..."
	exit 1
fi
selVendor="Qualcom"

allProject=(`$parseIni "field" "$upCfgFile" "CONFIGS" "QualcommProjects"` )
selProject="`GetChoice "请选择待打包的项目:" allProject 2`"
# ### init all params  ### 
projectID="`$parseIni "field" "$upCfgFile" "$selProject" "projectID"`"
sourceTopDir="`$parseIni "field" "$upCfgFile" "$selProject" "sourceTopDir"`"
otaTool="${sourceTopDir}/`$parseIni "field" "$upCfgFile" "$selProject" "otaTool"`"
hbootFile="${sourceTopDir}/`$parseIni "field" "$upCfgFile" "$selProject" "hbootFile"`"
radioFile="${sourceTopDir}/`$parseIni "field" "$upCfgFile" "$selProject" "radioFile"`"
hostLinuxDir="${sourceTopDir}/`$parseIni "field" "$upCfgFile" "$selProject" "hostLinuxDir"`"
signature="${sourceTopDir}/`$parseIni "field" "$upCfgFile" "$selProject" "signature"`"
sourceTargetFile="${sourceTopDir}/`$parseIni "field" "$upCfgFile" "$selProject" "sourceTargetFile"`"
fullUpdateFile="${sourceTopDir}/`$parseIni "field" "$upCfgFile" "$selProject" "fullUpdateFile"`"
releaseDir="`$parseIni "field" "$upCfgFile" "$selProject" "releaseDir"`"
packImgDir="`$parseIni "field" "$upCfgFile" "$selProject" "packImgDir"`"
hwVersion=`GetHardwareVersion $sourceTopDir $projectID`
swVersion=`GetSoftwareVersion $sourceTopDir $projectID`
modelName=`GetModelName $sourceTopDir $projectID`
echo "modelName=$modelName"
swVersion="`echo $swVersion | awk -F "_" '{print $2}' | tr -d 'V' `"
hbootVersion="`GetHbootVersion $sourceTopDir $projectID`"
radioVersion="`GetRadioVersion $sourceTopDir $projectID`"

releaseDir="${releaseDir}/update-${hwVersion}/"
mkdir -p $releaseDir

function ParseChecksumInfo()
{
	local infoF="$1"
	local tag="$2"
	cat "$infoF" | grep "$tag" | awk '{print $2}'
}

function GenUpdatePatchZip()
{
	local zipFrom="$1"
	local zipTo="$2"
	local target="$3"
	mkdir -p `dirname $target`
	EchoText "COMMAND IS >>>>"
	EchoText "$otaTool -i $zipFrom -p $hostLinuxDir -t MMC -k $signature $zipTo $target" GREEN
	EchoText "COMMAND END <<<<"
		$otaTool -i $zipFrom -p $hostLinuxDir -t MMC -k $signature $zipTo $target
}

if [ "`pwd`" != "$sourceTopDir"  ] && [ "`pwd`/" != "$sourceTopDir" ]; then
	EchoText "请务必在项目源码根目录:" RED
	EchoText "$sourceTopDir" WHITE
	EchoText "执行此命令!!!" RED
	exit 1
fi
if [ "$TARGET_PRODUCT" == "" ]; then
	EchoText "请先设定环境变量!!!"
	exit 1
fi

result=`Question "是否拷贝完整升级包及target-files" 1 "yes"`
if [ "$result" == "yes" ]; then 
	currentReleaseDir="${releaseDir}/${swVersion}/"
	mkdir -p ${currentReleaseDir}
	# ### confirm svn version  ###
	sourceUrl="`GetDirRepoUrl ${sourceTopDir}`"
	sourceSvnCommitVer="`GetSvnCommitted ${sourceTopDir}`"
	sourceHead="`GetRepoHeadVersion ${sourceUrl}`"
	sourceSvnVer="`InputNumber "请确定主线SVN版本号:"  "1" ${sourceHead} ${sourceSvnCommitVer}`"
	# ### cp files ###
	EchoText "STARTING TO COPY FILES "
	rm $currentReleaseDir/* -rf;  # ### clear previous files in any case ###
	upHboot="$currentReleaseDir/hboot_${hbootVersion}.img"
	upRadio="$currentReleaseDir/radio_${radioVersion}.img"
	upTargetfile="$currentReleaseDir/target-files_${swVersion}_r${sourceSvnVer}.zip"
	upUpdate="$currentReleaseDir/${modelName}-${hwVersion}-${hbootVersion}-update-full_${swVersion}_r${sourceSvnVer}.zip"
	cp ${hbootFile}			$upHboot -rfv
	cp ${radioFile}			$upRadio -rfv
	cp ${sourceTargetFile}	$upTargetfile -rfv
	cp ${fullUpdateFile}	$upUpdate -rfv
fi
cd $releaseDir
	allPreVersion=(`ls -trF | grep '/' |  tr "\n" " "| tr -d '/'`)
	allPreVerLen=${#allPreVersion[*]}
cd - > /dev/null

# ### gen update patches ### 
if [ $allPreVerLen -lt 1 ]; then
	EchoText "无较早的软件版本，无须制作增量升级包...\n\t脚本退出...."
	exit 0;
fi

# ### get current to patch ###
defaultVer=${#allPreVersion[*]}
selVer="`GetChoice "请指定待制作升级包的版本:" allPreVersion $defaultVer`"
echo "you selected $selVer"
if [ "$upHboot" == "" ] || [ "$upRadio" = "" ] || [ "$upTargetfile" == "" ] || [ "$upUpdate" == "" ];then
	upHboot="`ls $releaseDir/$selVer/hboot_*.img 2> /dev/null`"
	upRadio="`ls $releaseDir/$selVer/radio_*.img 2> /dev/null`"
	upTargetfile="`ls $releaseDir/$selVer/target-files_*.zip 2> /dev/null`"
	upUpdate="`ls $releaseDir/$selVer/*-update-full_${selVer}_*.zip 2> /dev/null`"
fi


#  ### confirm update.zip target_files boot.img checksum same with packImages ###
	EchoText "校验update.zip target_files boot.img 是否一致..."
	#update.zip

	packUpdate="`ls $packImgDir/images-${hwVersion}/image*-${hwVersion}_${selVer}/update_${hwVersion}_${selVer}_r*.zip 2> /dev/null`"
	EchoText "check $upUpdate $packUpdate"
	if [ "`CompareChecksum "$upUpdate" "$packUpdate"`" != "yes" ]; then
		EchoText "\t制作升级包的 [$upUpdate] 与 [$packUpdate] 校验值不一致!，退出制作升级包!"
		exit 1
	fi
	#target_files
	packTargetfile="`ls $packImgDir/images-${hwVersion}/image*-${hwVersion}_${selVer}/*-target_files-*_trunk.zip 2> /dev/null`"
	EchoText "check $upTargetfile $packTargetfile"
	if [ "`CompareChecksum "$upTargetfile" "$packTargetfile"`" != "yes" ]; then
		EchoText "\t制作升级包的 [$upTargetfile] 与 [$packTargetfile] 校验值不一致!退出制作升级包!"
		exit 1
	fi
	# unzip update.zip file
	tmpUpUpD="/tmp/upUpdateDir/"
	tmpPcakUpD="/tmp/packUpdateDir/"
	rm  $tmpUpUpD $tmpPcakUpD -rf
	mkdir -p $tmpUpUpD
	mkdir -p $tmpPcakUpD
	unzip $upUpdate 	-d $tmpUpUpD 	> /dev/null
	unzip $packUpdate 	-d $tmpPcakUpD 	> /dev/null
	#radio.img in update.zip
	EchoText "check $tmpUpUpD/radio.img $tmpPcakUpD/radio.img"
	if [ "`CompareChecksum "$tmpUpUpD/radio.img"  "$tmpPcakUpD/radio.img"`" != "yes"  ] ;then
		EchoText "\t制作升级包的 [$tmpUpUpD/radio.img] 与 [$tmpPcakUpD/radio.img] 校验值不一致!退出制作升级包!"
		exit 1		
	fi

	#boot.img in update.zip
	EchoText "check $tmpUpUpD/boot.img $tmpPcakUpD/boot.img sum"	
	if [ "`CompareChecksum "$tmpUpUpD/boot.img"  "$tmpPcakUpD/boot.img"`" != "yes"  ] ;then
		EchoText "\t制作升级包的 [$tmpUpUpD/boot.img] 与 [$tmpPcakUpD/boot.img] 校验值不一致!退出制作升级包!"
		exit 1		
	fi
	
	# radio.img、boot.img in tar.gz and update.zip
	# radio.img
	EchoText "对比update.zip中boot.img radio.img与发布压缩包中的是否一致..."
	packChksumFile="`ls $packImgDir/images-${hwVersion}/image*-${hwVersion}_${selVer}/${hwVersion}_${selVer}-checksum-trunk.info`"
	if [ -f "$packChksumFile" ];then
		#radio
		if [ "`GetSha1sum "$tmpUpUpD/radio.img"`" != "`ParseChecksumInfo "$packChksumFile" "^sha1sum.*amss.mbn"`" ] || [ "`GetMd5sum "$tmpUpUpD/radio.img"`" != "`ParseChecksumInfo "$packChksumFile" "^md5sum.*amss.mbn"`"  ]; then
			EchoText "\t压缩发布包文件、全量升级包两者radio.img校验值不一致!退出制作升级包!"
		fi
		#boot.img
		if [ "`GetSha1sum "$tmpUpUpD/boot.img"`" != "`ParseChecksumInfo "$packChksumFile" "^sha1sum.*boot.img"`" ] || [ "`GetMd5sum "$tmpUpUpD/boot.img"`" != "`ParseChecksumInfo "$packChksumFile" "^md5sum.*boot.img"`"  ]; then
			EchoText "\t压缩发布包文件、全量升级包两者boot.img校验值不一致!退出制作升级包!"
		fi
	else
		EchoText "\t无法获取压缩包校验值信息文件!退出制作升级包"
		exit 1
	fi
	
	
#  end of confirm checksum

allPreVer=(`echo ${allPreVersion[@]} | sed "s/${selVer}/ /g"` )
#echo "\${allPreVer[@]}=${allPreVer[@]}"
allSelPreVer=( `Question "请设定较旧的版本(多个版本间使用空格分开) " 2 "${allPreVer[*]}"`)
if [ "$sourceSvnVer" == "" ]; then
	sourceSvnVer="`ls ${releaseDir}/$selVer/target-files_${selVer}_r*.zip | awk -F '_' '{print $3}' | tr -d 'r' | tr -d '.zip'`"
fi
EchoText "开始制作 $selVer 对 ${allSelPreVer[@]} 的升级包"
	for v in ${allSelPreVer[@]}; do
		EchoText "------------版本$selVer 对 版本$v 的升级包------------\n-_-"
		if [ ! -f $releaseDir/$v/target-files_*.zip ]; then
			EchoText "版本 $v 无升级所需的压缩包文件!!!\n-_-||"
		else
			swVerTo="`ls $releaseDir/$v/target-files_*.zip | xargs basename | awk -F '_' '{print $2}'`"
			svnVerTo="`ls $releaseDir/$v/target-files_*.zip | xargs basename | awk -F '_' '{print $3}' | tr -d '.zip'`"
			updateTarget="${releaseDir}/$selVer/update-patch_${selVer}_for_${swVerTo}_r${sourceSvnVer}_for_${svnVerTo}.zip"
			GenUpdatePatchZip   $releaseDir/$v/target-files_*.zip ${releaseDir}/$selVer/target-files_${selVer}_r${sourceSvnVer}.zip ${releaseDir}/$selVer/${modelName}-${hwVersion}-${hbootVersion}-update-patch_${selVer}_for_${swVerTo}_r${sourceSvnVer}_for_${svnVerTo}.zip
		fi
	done
	GenChecksum "${releaseDir}/$selVer/" "${releaseDir}/$selVer/update-${hwVersion}_${selVer}-checksum.info"
EchoText "!!! ALL FINISHED !!!"
